package com.xjrsoft.module.customerTwo.AppManage.service_support_order.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaMode;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xjrsoft.common.page.PageOutput;
import com.xjrsoft.common.page.ConventPage;
import java.util.List;
import com.xjrsoft.common.result.Response;
import com.xjrsoft.core.tool.utils.BeanUtil;
import com.xjrsoft.module.base.service.IXjrBaseCodeRuleService;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.dto.ServiceSupportOrderDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.dto.SaveServiceSupportOrderFormDataDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.vo.ServiceSupportOrderFormDataVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.vo.ServiceSupportOrderListVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.dto.ServiceSupportOrderListDto;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.vo.ServiceSupportOrderTableVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.entity.ServiceSupportOrderTable;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.dto.ServiceSupportOrderTableDto;
import com.xjrsoft.core.secure.utils.SecureUtil;
import com.xjrsoft.core.tool.utils.FlowableUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;

import org.springframework.web.bind.annotation.*;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.entity.ServiceSupportOrder;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.vo.ServiceSupportOrderVo;
import com.xjrsoft.module.customerTwo.AppManage.service_support_order.service.IServiceSupportOrderService;

import javax.annotation.Resource;

/**
 *  控制器
 *
 * @author lisen
 * @since 2024-04-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/service_support_order")
@Api(value = "工单管理", tags = "工单管理接口")
public class ServiceSupportOrderController {


	@Resource
	IServiceSupportOrderService serviceSupportOrderService;

	private final IXjrBaseCodeRuleService codeRuleService;

	/**
	 * 详情 
	 */
	@GetMapping("/{id}")
	@ApiOperation(value = "详情", notes = "传入serviceSupportOrder")
	@SaCheckPermission(value = {"service_support_order:view", "service_support_order:modify"}, mode = SaMode.OR)
	public Response<ServiceSupportOrderFormDataVo> getFormData(@PathVariable String id) {
		ServiceSupportOrderFormDataVo formDataVo = new ServiceSupportOrderFormDataVo();
		// 主表数据
		ServiceSupportOrder serviceSupportOrder = serviceSupportOrderService.getById(id);
		ServiceSupportOrderVo serviceSupportOrderVo = BeanUtil.copy(serviceSupportOrder, ServiceSupportOrderVo.class);
		formDataVo.setServiceSupportOrderVo(serviceSupportOrderVo);
		// 从表数据
		List<ServiceSupportOrderTableVo> serviceSupportOrderTableVoList = BeanUtil.copyList(serviceSupportOrderService.getServiceSupportOrderTableByParentId(id), ServiceSupportOrderTableVo.class);
		formDataVo.setServiceSupportOrderTableVoList(serviceSupportOrderTableVoList);
		return Response.ok(formDataVo);
	}

	/**
	 * 自定义分页 
	 */
	@GetMapping
	@ApiOperation(value = "分页", notes = "传入serviceSupportOrder")
		public Response<PageOutput<ServiceSupportOrderListVo>> getPageList(ServiceSupportOrderListDto listDto) {
		IPage<ServiceSupportOrder> page = serviceSupportOrderService.getPageList(listDto);
		List<ServiceSupportOrderListVo> records = BeanUtil.copyList(page.getRecords(), ServiceSupportOrderListVo.class);
			// 工作流数据处理
		FlowableUtil.systemFormList(records, SecureUtil.getModuleId());
		return Response.ok(ConventPage.getPageOutput(page.getTotal(), records));
	}

	/**
	 * 新增 
	 */
	//用户提交工单
	@PostMapping
	@ApiOperation(value = "新增", notes = "传入serviceSupportOrder")
	@SaCheckPermission("service_support_order:add")
	public Response save(@RequestBody SaveServiceSupportOrderFormDataDto formDto) {
		ServiceSupportOrderDto serviceSupportOrderDto = formDto.getServiceSupportOrderDto();
		ServiceSupportOrder serviceSupportOrder = BeanUtil.copy(serviceSupportOrderDto, ServiceSupportOrder.class);
		String dataId = serviceSupportOrderService.addServiceSupportOrder(serviceSupportOrder);
		if (dataId != null) {
			codeRuleService.useEncode("ServiceSupportOrder");
		}
		return dataId == null ? Response.notOk() : Response.ok(dataId);
	}

	/**
	 * 修改 
	 */
	@PutMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入serviceSupportOrder")
	@SaCheckPermission("service_support_order:modify")
	public Response update(@PathVariable String id, @RequestBody SaveServiceSupportOrderFormDataDto formDto) {
		ServiceSupportOrderDto serviceSupportOrderDto = formDto.getServiceSupportOrderDto();
		ServiceSupportOrder serviceSupportOrder = BeanUtil.copy(serviceSupportOrderDto, ServiceSupportOrder.class);

		List<ServiceSupportOrderTableDto> serviceSupportOrderTableDtoList = formDto.getServiceSupportOrderTableDto();
		List<ServiceSupportOrderTable> serviceSupportOrderTableList = BeanUtil.copyList(serviceSupportOrderTableDtoList, ServiceSupportOrderTable.class);
		return Response.status(serviceSupportOrderService.updateServiceSupportOrder(id, serviceSupportOrder, serviceSupportOrderTableList));
	}



	/**
	 * 删除 
	 */
	@DeleteMapping("/{ids}")
	@ApiOperation(value = "删除", notes = "传入ids")
	@SaCheckPermission(value = {"service_support_order:batchDelete", "service_support_order:delete"}, mode = SaMode.OR)
	public Response remove(@ApiParam(value = "主键集合", required = true) @PathVariable String ids) {
		return Response.status(serviceSupportOrderService.delete(ids));
	}

	@PostMapping("/{id}")
	@ApiOperation(value = "修改", notes = "传入serviceSupportOrder")
	@SaCheckPermission("service_support_order:modify")
	public Response confirm(@PathVariable Integer id, @RequestParam boolean bl) {
		return Response.status(serviceSupportOrderService.confirm(id, bl));
	}

}
